package car.aistar.controller;

import car.aistar.dto.CarOverview;
import car.aistar.dto.PriceCutXu;
import car.aistar.dto.Result;
import car.aistar.entity.UserDetail;
import car.aistar.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName PersonalViewController
 * @Description TODO 个人中心页面数据
 * @Author laoxu
 * @Date 2020/10/4 13:29
 * Version 1.0
 **/

@Controller
@RequestMapping("/Personal")
public class PersonalViewController {

    @Autowired
    private CarOverviewService carOverviewService;

    @Autowired
    private UserDetailService userDetailService;

    @Autowired
    private UserCollectService userCollectService;

    @Autowired
    private UserRecordsService userRecordsService;

    @Autowired
    private PriceCutService priceCutService;

    @Autowired
    private LaoxuPersonService laoxuPersonService;

    //用户收藏
    @RequestMapping("/Store")
    public String showCollect(Model model, String pageNow, String pageSize, HttpServletRequest request){
//        Long uid = 1L;
        //强转为Integer
        Integer id = (Integer) request.getSession().getAttribute("uid");
        String i = id+"";
        Long uid = Long.valueOf(i);
        System.err.println("用户id等于："+uid);
        UserDetail userDetail = userDetailService.queryById(uid);
        model.addAttribute("detail",userDetail);
        pageNow = pageNow==null?"1":pageNow;
        if(pageSize == null  || "-1".equals(pageSize)){
            pageSize = "5";
        }
        List<CarOverview> carOverviewList = carOverviewService.queryCollect(uid);
        PageInfo<CarOverview> carOverviewPageInfo= carOverviewService.findCollectPage(uid,pageNow,pageSize);
        model.addAttribute("pageColl",carOverviewPageInfo);
        model.addAttribute("collViews",carOverviewList);
        return "perCenter/store";

    }


    //用户历史浏览
    @RequestMapping("/history")
    public String showOver(Model model, String pageNow, String pageSize,HttpServletRequest request) {

        //强转为Integer
        Integer id = (Integer) request.getSession().getAttribute("uid");
        String i = id+"";
        Long uid = Long.valueOf(i);
//        Long uid = 1L;
        pageNow = pageNow == null ? "1" : pageNow;
        if (pageSize == null || "-1".equals(pageSize)) {
            pageSize = "6";
        }
        PageInfo<CarOverview> historyPageInfo = laoxuPersonService.queryPageHistory(uid, Integer.valueOf(pageNow),Integer.valueOf(pageSize));
        model.addAttribute("page", historyPageInfo);//有值
        System.err.println();
        return "perCenter/content";
    }

    /**
     *@Author laoxu
     *@Description //TODO 个人中心
     *@Date 11:01 2020/10/13
     *@Param [model]
     *@return java.lang.String
     **/
    @RequestMapping("/userDet")
    public String queryUser(Model model,HttpServletRequest request){
        //强转为Integer

        Integer id = (Integer) request.getSession().getAttribute("uid");
        System.err.println(id);
        String i = id+"";
        Long uid = Long.valueOf(i);
//        Long uid = 1L;
        UserDetail userDetail = userDetailService.queryById(uid);
        model.addAttribute("detail",userDetail);
        return "perCenter/perIndex";
    }

    //降价提醒
    @RequestMapping("/reduce")
    public String showPriceCut(Model model, String pageNow, String pageSize,HttpServletRequest request){
//        Long uid = 1L;

        //强转为Integer
        Integer id = (Integer) request.getSession().getAttribute("uid");
        String i = id+"";
        Long uid = Long.valueOf(i);
        UserDetail userDetail = userDetailService.queryById(uid);
        pageNow = pageNow == null ? "1" : pageNow;
        if (pageSize == null || "-1".equals(pageSize)) {
            pageSize = "5";
        }
        model.addAttribute("detail",userDetail);
        List<PriceCutXu> priceCutXuList = carOverviewService.queryPriceCut(uid);
        PageInfo<PriceCutXu> priceCutXuPageInfo = carOverviewService.priceCutPage(uid,pageNow,pageSize);
        model.addAttribute("priCuts",priceCutXuList);
        model.addAttribute("pageCutp",priceCutXuPageInfo);
        return "perCenter/reduce";
    }
    
    /**
    *@Author laoxu
    *@Description //TODO 取消收藏 满足用户id 与车辆id两个条件删除收藏表记录
    *@Date 7:11 2020/10/5
    *@Param 
    *@return 
    **/
    @PostMapping("/Unfavorite")
    public @ResponseBody
    Result unfavorite(String id,HttpServletRequest request){
        //传入车辆id

        //强转为Integer
        Integer d = (Integer) request.getSession().getAttribute("uid");
        String i = d+"";
        Long uid = Long.valueOf(i);
        Boolean flage = userCollectService.deleteById1(Long.valueOf(id),uid);
        return new Result("200","成功",flage);
    }
    
    /**
    *@Author laoxu
    *@Description //TODO 删除浏览记录 满足用户id 与车辆id两个条件删除浏览记录
    *@Date 7:46 2020/10/5
    *@Param 
    *@return 
    **/
    @PostMapping("/delHistory")
    public @ResponseBody
    Result delHistory(String id, HttpServletRequest request){
        //传入车辆id
        //强转为Integer
        Integer d = (Integer) request.getSession().getAttribute("uid");
        String i = d+"";
        Long uid = Long.valueOf(i);
//        Long uid = 1L;
        Boolean flage = userRecordsService.deleteById1(uid,Long.valueOf(id));
        return new Result("200","成功",flage);
    }

    /**
    *@Author laoxu
    *@Description //TODO 取消降价提醒
    *@Date 8:01 2020/10/5
    *@Param
    *@return
    **/
    @PostMapping("/delReduce")
    public @ResponseBody
    Result delReduce(String id,HttpServletRequest request){
        //传入车辆id
//        String uid = "1";

        //强转为Integer
        Integer d = (Integer) request.getSession().getAttribute("uid");
        String i = d+"";
        Long uid = Long.valueOf(i);
        Boolean flage = priceCutService.deleteById1(uid,Long.valueOf(id));
        return new Result("200","成功",flage);
    }
}
